﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MigraDoc.DocumentObjectModel;
using Payroll.BusinessLogic.CustomerManager;
using Payroll.BusinessLogic.Utils;
using Payroll.Common.Utils;
using PayrollHR.Web;
using Payroll.BusinessLogic.EmployeeManagement;
using System.Text;
using Payroll.BusinessLogic.Data;
using Payroll.BusinessLogic;


namespace PayrollHR.Administration.Modules
{
    public partial class EmployeeAdd : BasePayrollAdministrationUserControl
    {
        public string SiteMapProviderName
        {
            get
            {
                return Convert.ToString(ViewState["SiteMapProviderName"]);
            }
            set
            {
                ViewState["SiteMapProviderName"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindEmployee();
                BindMenu();
            }
        }
        protected void BindMenu()
        {
            var permisionColection = this.EmployeeService.GetPermisionById(this.EmployeeID);
            string SiteMapProviderName = "PayrollAdminXmlSiteMapProvider";
            SiteMapProvider siteMapProvider = SiteMap.Providers[SiteMapProviderName];
            if (siteMapProvider == null)
            {
                Visible = false;
            }
            else
            {
                foreach (SiteMapNode node in siteMapProvider.RootNode.ChildNodes)
                {
                    TreeNode rootNode = new TreeNode(node.Title, node.ResourceKey);
                    rootNode.Checked = this.FindMenu(node.ResourceKey, permisionColection);
                    MenuTreeView.Nodes.Add(rootNode);
                    BuildMenuRecursive(rootNode, node, permisionColection);
                }
            }
        }
        private void BuildMenuRecursive(TreeNode currentNode, SiteMapNode node, List<Permision> permisionColection)
        {
            if (node.HasChildNodes)
            {
                foreach (SiteMapNode childNode in node.ChildNodes)
                {
                    TreeNode nodeTree = new TreeNode(childNode.Title, childNode.ResourceKey);
                    nodeTree.Checked = this.FindMenu(node.ResourceKey, permisionColection);
                    currentNode.ChildNodes.Add(nodeTree);
                    BuildMenuRecursive(nodeTree, childNode, permisionColection);
                }
            }
        }
        protected bool Find(List<Permision> List, string page)
        {

            return true;
        }
        protected string GetPageId(string url)
        {
            int index = url.LastIndexOf('/');
            if (index > 0)
            {
                url = url.Substring(index, url.Length - index);
            }
            return url;
        }
        protected List<Permision> GetPermision()
        {
            int employeeId = PayrollContext.Current.Session.EmployeeId;
            List<Permision> List = new List<Permision>();
            foreach (TreeNode note in MenuTreeView.CheckedNodes)
            {
                if (List.Count == 0)
                {
                    List.Add(new Permision { EmployeeId = employeeId, PageId = note.Value });
                    continue;
                }
                TreeNode parentNote = note.Parent;
                if (!CommonHelper.IsNull(parentNote))
                {
                    if (!this.FindMenu(parentNote.Value, List))
                    {
                        List.Add(new Permision { EmployeeId = employeeId, PageId = parentNote.Value });
                    }
                }
                List.Add(new Permision { EmployeeId = employeeId, PageId = note.Value });
            }
            return List;
        }
        private bool FindMenu(string page, List<Permision> List)
        {
            return List.FindIndex(item => item.PageId.ToLower().Equals(page.ToLower())) >= 0 ? true : false;
        }
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    lblEmployeeExists.Visible = false;
                    Save();
                    Response.Redirect("ListEmployee.aspx");
                }
                catch (Exception exc)
                {
                    lblEmployeeExists.Visible = true;
                    ProcessException(exc);
                }
            }
        }
        private void BindEmployee()
        {
            if (this.EmployeeID == 0)
            {
                return;
            }
            var _employee = this.EmployeeService.GetEmployeeById(this.EmployeeID);
            if (CommonHelper.IsNull(_employee))
            {
                return;
            }
            ctrlEmployeeInfo.bindData(_employee);
        }
        private int Save()
        {
            int employeeid = 0;
            List<Permision> List = this.GetPermision();
            var _employee = ctrlEmployeeInfo.getEmployee();
            if (_employee.Name == "" | _employee.Username == "")
            {
                return employeeid;
            }
            if (this.EmployeeID == 0)
            {

                employeeid = this.EmployeeService.SaveEmployee(_employee, List);
            }
            else
            {
                _employee.EmployeeId = this.EmployeeID;
                employeeid = this.EmployeeService.UpdateEmployee(_employee, List);
            }
            return employeeid;
        }

        public int EmployeeID
        {
            get
            {
                return CommonHelper.QueryStringInt("Id");
            }
        }
        protected void SaveAndStayButton_Click(object sender, EventArgs e)
        {
            int employeeid = Save();
            if (Page.IsValid)
                Response.Redirect(string.Format("addEmployee.aspx?id={0}", employeeid));
        }
    }
}